import java.util.List;

public class GetIntersectionNode {
    /**
     * 输入两个链表，找出它们的第一个公共节点。
     * @param headA
     * @param headB
     * @return
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        /***
         * step1 计算A,B两链表的长度
         */
        if (headA==null||headB==null)
            return null;
        int lenA = 0;
        int lenB = 0;
        ListNode ptrA = headA;
        while (ptrA!=null){
            ptrA = ptrA.next;
            lenA++;
        }
        ListNode ptrB = headB;
        while (ptrB!=null){
            ptrB = ptrB.next;
            lenB++;
        }
        if (lenA<lenB){
            int len = lenB-lenA;
            ptrB =headB;
            while (len>0){
                ptrB= ptrB.next;
                len--;
            }
            ptrA = headA;
            while (ptrA!=ptrB){
                ptrA = ptrA.next;
                ptrB = ptrB.next;
            }
            return ptrA;
        }else {
            int len = lenA - lenB;
            ptrA = headA;
            while (len>0){
                ptrA= ptrA.next;
                len--;
            }
            ptrB = headB;
            while (ptrA!=ptrB){
                ptrA = ptrA.next;
                ptrB = ptrB.next;
            }
            return ptrA;
        }







    }
}
